package com.ruoyi.web.service.Impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.pinyin.PinYinUtils;
import com.ruoyi.web.domain.MesPlaceorderSetupinfo;
import com.ruoyi.web.mapper.MesConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.web.domain.MesPlaceorderMachineinfo;
import com.ruoyi.web.mapper.MesPlaceorderInfoMapper;
import com.ruoyi.web.domain.MesPlaceorderInfo;
import com.ruoyi.web.service.IMesPlaceorderInfoService;

import javax.annotation.Resource;

/**
 * 下单标准基本信息Service业务层处理
 * 
 * @author wxt
 * @date 2024-01-09
 */
@Service
public class MesPlaceorderInfoServiceImpl implements IMesPlaceorderInfoService 
{
    @Autowired
    private MesPlaceorderInfoMapper mesPlaceorderInfoMapper;

    @Resource
    private MesConfigMapper configMapper;

    /**
     * 查询下单标准基本信息
     * 
     * @param id 下单标准基本信息主键
     * @return 下单标准基本信息
     */
    @Override
    public MesPlaceorderInfo selectMesPlaceorderInfoById(Long id)
    {
        MesPlaceorderInfo data= mesPlaceorderInfoMapper.selectMesPlaceorderInfoById(id);
        return data;
    }

    /**
     * 查询下单标准基本信息列表
     * 
     * @param mesPlaceorderInfo 下单标准基本信息
     * @return 下单标准基本信息
     */
    @Override
    public List<MesPlaceorderInfo> selectMesPlaceorderInfoList(MesPlaceorderInfo mesPlaceorderInfo)
    {
        return mesPlaceorderInfoMapper.selectMesPlaceorderInfoList(mesPlaceorderInfo);
    }
    @Override
    public int getstatus(String type)
    {
        int count= mesPlaceorderInfoMapper.getstatus(type);
        return  count;
    }
    @Override
    public BigDecimal selectPutratio(Map<String,Object> params)
    {
        BigDecimal putRatio= mesPlaceorderInfoMapper.selectPutratio(params);
        return  putRatio;
    }
    @Override
    public int getmachine(String machineId){
        return mesPlaceorderInfoMapper.getmachine(machineId);
    }
    /**
     * 新增下单标准基本信息
     * 
     * @param mesPlaceorderInfo 下单标准基本信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMesPlaceorderInfo(MesPlaceorderInfo mesPlaceorderInfo) {
        mesPlaceorderInfo.setCreateTime(DateUtils.getNowDate());

        Map<String, Object> map = new HashMap<>();
        map.put("columnName", "order_num");
        map.put("tableName", "mes_placeorder_info");
        int maxNum = configMapper.getMaxId(map);

        String code = PinYinUtils.getCodeNormal(maxNum, "XDBZ");
        mesPlaceorderInfo.setOrderNum(code);

//        List<MesPlaceorderMachineinfo> mesMachineinfoList = mesPlaceorderInfo.getMesPlaceorderMachineinfoList();
//        if (StringUtils.isNotNull(mesMachineinfoList)) {
//            List<MesPlaceorderMachineinfo> list = new ArrayList<MesPlaceorderMachineinfo>();
//            for (MesPlaceorderMachineinfo Machineinfo : mesMachineinfoList){
//              int count= mesPlaceorderInfoMapper.getmachine(Machineinfo.getMachineid());
//              if (count>0){
//                  return "部分机台已被引用，请重新选择！";
//              }
//        }
//    }

        int rows = mesPlaceorderInfoMapper.insertMesPlaceorderInfo(mesPlaceorderInfo);
        insertMesPlaceorderMachineinfo(mesPlaceorderInfo);
        insertMesPlaceorderSetupinfo(mesPlaceorderInfo);
        return rows;
    }

    /**
     * 修改下单标准基本信息
     * 
     * @param mesPlaceorderInfo 下单标准基本信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMesPlaceorderInfo(MesPlaceorderInfo mesPlaceorderInfo)
    {
        String username = SecurityUtils.getUsername();
        mesPlaceorderInfo.setUpdateBy(username);
        mesPlaceorderInfo.setUpdateTime(DateUtils.getNowDate());

        mesPlaceorderInfoMapper.deleteMesPlaceorderMachineinfoByPlaceorderid(mesPlaceorderInfo.getId());
        mesPlaceorderInfoMapper.deleteMesPlaceorderSetupinfoByPlaceorderid(mesPlaceorderInfo.getId());
        insertMesPlaceorderMachineinfo(mesPlaceorderInfo);
        insertMesPlaceorderSetupinfo(mesPlaceorderInfo);
        return mesPlaceorderInfoMapper.updateMesPlaceorderInfo(mesPlaceorderInfo);
    }

    @Transactional
    @Override
    public int updatestatus(MesPlaceorderInfo mesPlaceorderInfo){
        return mesPlaceorderInfoMapper.updatestatus(mesPlaceorderInfo);
    };

    /**
     * 批量删除下单标准基本信息
     * 
     * @param ids 需要删除的下单标准基本信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesPlaceorderInfoByIds(Long[] ids)
    {
        mesPlaceorderInfoMapper.deleteMesPlaceorderMachineinfoByPlaceorderids(ids);
        mesPlaceorderInfoMapper.deleteMesPlaceorderSetupinfoByPlaceorderids(ids);
        return mesPlaceorderInfoMapper.deleteMesPlaceorderInfoByIds(ids);
    }

    /**
     * 删除下单标准基本信息信息
     * 
     * @param id 下单标准基本信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesPlaceorderInfoById(Long id)
    {
        mesPlaceorderInfoMapper.deleteMesPlaceorderMachineinfoByPlaceorderid(id);
        mesPlaceorderInfoMapper.deleteMesPlaceorderSetupinfoByPlaceorderid(id);
        return mesPlaceorderInfoMapper.deleteMesPlaceorderInfoById(id);
    }


    /**
     * 新增下单标准基本信息详情_相关机台信息
     * 
     * @param mesPlaceorderInfo 下单标准基本信息对象
     */
    public void insertMesPlaceorderMachineinfo(MesPlaceorderInfo mesPlaceorderInfo)
    {
        List<MesPlaceorderMachineinfo> mesPlaceorderMachineinfoList = mesPlaceorderInfo.getMesPlaceorderMachineinfoList();
        Long id = mesPlaceorderInfo.getId();
        if (StringUtils.isNotNull(mesPlaceorderMachineinfoList))
        {
            List<MesPlaceorderMachineinfo> list = new ArrayList<MesPlaceorderMachineinfo>();
            for (MesPlaceorderMachineinfo mesPlaceorderMachineinfo : mesPlaceorderMachineinfoList)
            {
                mesPlaceorderMachineinfo.setPlaceorderid(id);
                list.add(mesPlaceorderMachineinfo);
            }
            if (list.size() > 0)
            {
                mesPlaceorderInfoMapper.batchMesPlaceorderMachineinfo(list);
            }
        }
    }

    /**
     * 新增下单标准基本信息_设置明细信息
     *
     * @param mesPlaceorderInfo 下单标准基本信息对象
     */
    public void insertMesPlaceorderSetupinfo(MesPlaceorderInfo mesPlaceorderInfo)
    {
        List<MesPlaceorderSetupinfo> mesPlaceorderSetupinfoList = mesPlaceorderInfo.getMesPlaceorderSetupinfoList();
        Long id = mesPlaceorderInfo.getId();
        if (StringUtils.isNotNull(mesPlaceorderSetupinfoList))
        {
            List<MesPlaceorderSetupinfo> list = new ArrayList<MesPlaceorderSetupinfo>();
            for (MesPlaceorderSetupinfo mesPlaceorderSetupinfo : mesPlaceorderSetupinfoList)
            {
                mesPlaceorderSetupinfo.setPlaceorderid(id);
                list.add(mesPlaceorderSetupinfo);
            }
            if (list.size() > 0)
            {
                mesPlaceorderInfoMapper.batchMesPlaceorderSetupinfo(list);
            }
        }
    }

}
